package tree.easy;

import tree.TreeNode;

/**
 * 530. 二叉搜索树的最小绝对差
 */
public class getMinimumDifference {
    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(90, new TreeNode(69, new TreeNode(49,null,new TreeNode(52)), new TreeNode(89)), null);
        System.out.println(new getMinimumDifference().getMinimumDifference(treeNode));
    }
    int min;
    int pre;
    public int getMinimumDifference(TreeNode root) {
        min = Integer.MAX_VALUE;
        pre = -1;
        dfs(root);
        return min;
    }

    public void dfs(TreeNode root) {
        if (root == null) {
            return;
        }
        dfs(root.left);
        if (pre == -1) {
            pre = root.val;
        } else {
            min = Math.min(root.val - pre, min);
            pre = root.val;
        }
        dfs(root.right);
    }
}
